<template>
	<view class="content">
		<view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: tabCurrentTwoIndex === index}"
				@click="tabClick(index)"
			>
				{{item.text}}({{navList[index].listData.length}})
			</view>
			
		</view>

		<swiper :current="tabCurrentTwoIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadNextPage">
					<!-- 空白页 -->
						<empty v-if="tabItem.loaded === true && tabItem.length==0"></empty>

					<!-- 订单列表 -->
					<view class="">
						<view class="list" v-for="(item,index) in tabItem.listData" :key="index" @click="navTo('rateDetail',item.goodsCommentId)">
							<view class="title">
								<view class="title-info">
									<image :src="item.avatar"></image>
									<text class="colorfont font28">{{item.nickName}}</text>
								</view>
								<text class="font20 color999">{{item.evaluateTime | timeStamp}}</text>
							</view>
							<view class="detail">
								<view class="colorfont font28">{{item.commentContent==''?'暂无评价':item.commentContent}}</view>
								<view class="imgs">
									<image src="../../../static/emptyCart.jpg" mode=""></image>
								</view>
								<view class="spec">
									<text class="font24 color999">规格：{{item.goodsList}}</text>
									<view class="spec-info">
										<image src="../../../static/home/order/good_on.png" mode="widthFix" v-if="item.commentLevel==3"></image>
										<image src="../../../static/home/order/middle_on.png" mode="widthFix" v-else-if="item.commentLevel==2"></image>
										<image src="../../../static/home/order/bad_on.png" mode="widthFix" v-else-if="item.commentLevel==1"></image>
										<text class="font24 colorfont">{{item.commentLevel==3?'好评':item.commentLevel==2?'中评':item.commentLevel==1?'差评':''}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty";
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				tabCurrentTwoIndex: 0,
				navList: [{
						text: '全部',
						listData: []
					},
					{
						text: '好评',
						listData: []
					},
					{
						text: '中评',
						listData: []
					},
					{
						text: '差评',
						listData: []
					}
				],
				listDataOld: [], //临时存储数据
				loadingType: 'noMore', //
				hiddenMore: false, //隐藏更多控件
				pageNo: 1, //当前页
				pageNos: 0,
				hiddenPage: true,
				optionData:{},//传过来的数据
			};
		},
		//下拉刷新，需要自己在page.json文件中配置开启页面下拉刷新 "enablePullDownRefresh": true
		onPullDownRefresh() {
			this.loadData();
			this.pageNo = 1
		},
		onLoad(options) {
			this.optionData = options;
			this.loadData()
		},
		methods: {
			//获取订单列表
			loadData(source) {
				let that = this;
				let nvaList = that.navList;
				let navItem = nvaList[that.tabCurrentTwoIndex];
				let indexSort = 0;
				if(that.tabCurrentTwoIndex==0){
					indexSort = 4;
				}else if(that.tabCurrentTwoIndex==1){
					indexSort = 3;
				}else if(that.tabCurrentTwoIndex==2){
					indexSort = 2;
				}else if(that.tabCurrentTwoIndex==3){
					indexSort = 1;
				}
				if (source === 'tabChange' && navItem.loaded === true) {
					return;
				}
				uni.showLoading({
					title: '加载中',
				})
				that.$http.request({
						method: 'POST', // 请求方法必须大写
						url: that.$requestApi.commentList,
						data: {
							cid:that.optionData.cid,
							commentLevel: indexSort,
							goodsId:that.optionData.goodsId,
							page: that.pageNo,
							size: 10
						}
					})
					.then(result => {
						uni.hideLoading()
						uni.stopPullDownRefresh();
						let _data = result.data;
						if (_data.code === 200) {
							if (that.pageNo <= 1) {
								that.listDataOld = _data.result.dataList;
							} else {
								that.listDataOld = that.listDataOld.concat(_data.result.dataList);
							}
							console.log(_data.result)
							that.pageNos = _data.result.totalPage;
							that.pageNo = _data.result.currentPage;
							let orderAllList = navItem.listData;
							that.listDataOld.forEach(item => {
								navItem.listData.push(item);
							})
							//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
							this.$set(navItem, 'loaded', true);
							that.navList = nvaList;
						}
					})
			},
			/**
			 * 获取下一页
			 */
			loadNextPage() {
				let that = this;
				if (that.pageNo >= that.pageNos) {
					that.hiddenMore = true;
					return;
				}
				//加载我的订单
				that.pageNo++;
				that.loadData();
			},
			
			navTo(url, id) {
				this.$Router.push({
					name: url,
					params: {
						goodsId: id,
						type:0//从列表进去判断
					}
				})
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentTwoIndex = e.target.current;
				this.pageNo = 1;
				this.loadData('tabChange');
			},
			
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentTwoIndex = index;
				this.pageNo = 1;
			},
			
		},
	}
</script>

<style lang="scss">
	page,
	.content {
		background:#fff;
		height: 100%;
	}

	.swiper-box {
		height: calc(100% - 120upx);
	}

	.list-scroll-content {
		height: 100%;
	}

	.navbar-two {
		display: flex;
		height: 120upx;
		padding: 0 30upx;
		background: #fff;
		position: relative;
		align-items: center;
		justify-content: space-between;
		z-index: 1;

		.nav-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 50upx;
			width: 120upx;
			font-size: 24upx;
			color: #8E8E93;
			border: 1upx solid rgba(112, 112, 112, 1);
			border-radius: 40upx;
			position: relative;

			&.current {
				background: rgba(255, 215, 193, 1);
				color: #FF5E00;
				border: 1px solid rgba(255, 93, 0, 1);
			}
		}
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0px 10upx 20upx rgba(0, 0, 0, 0.04);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: #8E8E93;
			position: relative;

			&.current {
				color: #0B0E11;
				font-weight: bold;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid #FF2300;
				}
			}
		}
	}

	.uni-swiper-item {
		height: auto;
	}
	.list{
		padding: 36rpx;
		position: relative;
		width: 100%;
		border-bottom: 1rpx solid rgba(245,245,250,1);
	.title {
		display: flex;
		align-items: center;
		justify-content: space-between;
	
		.title-info {
			display: flex;
			align-items: center;
	
			image {
				width: 60upx;
				height: 60upx;
				border-radius: 50%;
				margin-right: 20upx;
			}
		}
	}
	.detail{
		padding-left: 80upx;
		padding-top: 30upx;
		.imgs{
			image{
				width: 140upx;
				height: 140upx;
				border-radius: 8upx;
				margin-right: 36upx;
			}
			margin-top: 18upx;
		}
	}
	.spec{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 18upx;
		.spec-info{
			display: flex;
			align-items: center;
			image{
				width: 26upx;
				height: auto;
				margin-right: 10upx;
			}
		}
	}
	}
</style>
